package com.gxaedu.dly2022.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxaedu.dly2022.pojo.TestData;
import com.gxaedu.dly2022.service.TestDataService;
import com.gxaedu.dly2022.vo.MyReturnData;
import com.gxaedu.dly2022.vo.TestUser;
import org.omg.PortableServer.THREAD_POLICY_ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import java.io.FileNotFoundException;
import java.util.*;

/**
 * @author thinknovo
 * @version 1.0
 */
@RestController
public class TestDataController {

    @Resource
    private TestDataService testDataService;

    @RequestMapping("/test01")
    public List<TestData> test01(@RequestParam(name="myId", required = true) int id) {
        System.out.println("test01 id=" + id);
        return null;
    }

    @RequestMapping("/test02")
    public List<TestData> test02(@RequestParam(name="myId", required = true) int id,
                                 @RequestParam(name="myName", required = false, defaultValue = "2") int name) {
        System.out.println("test02 id=" + id + " name=" + name);
        return null;
    }

    @RequestMapping("/test03")
    public List<TestData> test03(@RequestParam(name="myId", required = true) int id, String name) {
        System.out.println("test03 id=" + id + " name=" + name);
        return null;
    }

    @RequestMapping("/test04")
    public List<TestData> test04(@RequestBody TestData testData) {
        System.out.println("test04 testData=" + testData);
        return null;
    }



    @RequestMapping("/hello")
    public String hello() {
        return "hello spring mvc";
    }

    @RequestMapping("/selectById")
    public List<TestData> selectById(@RequestParam(name="myId", required = false, defaultValue = "0") int id) {
        return testDataService.selectById();
    }

    @RequestMapping("/testJSON")
    public Map<String, Object> testJSON() throws NumberFormatException {

        Integer.parseInt("a");

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", 10000);     // 总计有多少条数据
        List<TestUser> listObj = new ArrayList<>();
        TestUser testData = null;
        for(int i = 0; i < 10; i++) {
            testData = new TestUser();
            testData.setId("10001" + i);
            testData.setUsername("杜甫" + i);
            testData.setEmail("test@email.com");
            testData.setSex("男");
            testData.setCity("浙江杭州");
            listObj.add(testData);
        }
        map.put("data", listObj);
        return map;
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        TestData testData = new TestData();
        testData.setId(1);
        testData.setUsername("gxa");
        String string = objectMapper.writeValueAsString(testData);   // 把一个java对象，转换为一个json对象
        System.out.println(string);

        String[] strArray = {"1", "2", "3", "4"};
        string = objectMapper.writeValueAsString(strArray);         // 把一个java数组对象，转换为一个json对象
        System.out.println(string);

        Map<String, String> map = new HashMap<>();
        map.put("username", "gxa");
        map.put("password", "123456");
        string = objectMapper.writeValueAsString(map);              // 把一个java map对象，转换为一个json对象
        System.out.println(string);

        Set<String> set = new HashSet<>();
        set.add("a");
        set.add("b");
        set.add("c");
        string = objectMapper.writeValueAsString(set);              // 把一个java set对象，转换为一个json对象
        System.out.println(string);

        List<String> list = new ArrayList<>();
        list.add("gxa01");
        list.add("gxa02");
        list.add("gxa03");
        string = objectMapper.writeValueAsString(list);              // 把一个java list对象，转换为一个json对象
        System.out.println(string);

        List<TestData> listObj = new ArrayList<>();
        listObj.add(testData);
        listObj.add(new TestData());
        string = objectMapper.writeValueAsString(listObj);              // 把一个java list obj对象，转换为一个json对象
        System.out.println(string);

        MyReturnData myReturnData = new MyReturnData();
        myReturnData.setCode(200);
        myReturnData.setMsg("查询成功");
        myReturnData.setList(listObj);
        string = objectMapper.writeValueAsString(myReturnData);              // 把一个java list obj对象，转换为一个json对象
        System.out.println(string);
    }

}
